package testGuava.example.testGuava;

import com.google.common.base.CaseFormat;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName : org.example.testGuava.StringTester
 * @Description : 类描述
 * Created by cyd on 2021-09-14 20:50:36
 */
public class StringTester {

    @Test
    public void testJoiner() {
        ArrayList<String> list = new ArrayList<>();
        list.add("s");
        list.add("a");
        String join = Joiner.on("-").skipNulls().join(list);
        System.out.println(join);
    }

    @Test
    public void testSplitter() {
        String str = "h-e-l-l-o- -2";
        Iterable<String> split1 = Splitter.on("-").split(str);
        Iterable<String> split2 = Splitter.on("-").omitEmptyStrings().split(str);
        Iterable<String> split3 = Splitter.on("-").omitEmptyStrings().trimResults().split(str);

        System.out.println(split1);
        System.out.println(split2);
        System.out.println(split3);
        System.out.println("--------------------");
        List<String> list = Splitter.on("-").omitEmptyStrings().splitToList(str);
        List<String> list2 = Splitter.on("-").trimResults().splitToList(str);
        System.out.println(list.size());
        System.out.println(list2.size());
        // omitEmptyStrings().trimResults() 这两个要一起用
        List<String> list3 = Splitter.on("-").omitEmptyStrings().trimResults().splitToList(str);
        System.out.println(list3.size());
    }

    /**
     * 驼峰和下划线的互转
     */
    @Test
    public void testTrans() {
        String str = "student_name";
        String camelStr = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, str);
        System.out.println(camelStr);

        String underStr = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, camelStr);
        System.out.println(underStr);
    }

}
